package com.padgett.resourcemanagement;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.padgett.resourcemanagement.data.ClientDAO;
import com.padgett.resourcemanagement.util.SystemConstants;

/**
 * Get Users
 * 
 * @author andyleung
 * 
 */
@SuppressWarnings("serial")
public class UsersJSP extends HttpServlet{
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {
		UserService userService=UserServiceFactory.getUserService();
		User user=userService.getCurrentUser();
		Key userKey=new KeyFactory.Builder("User",user.getEmail()).getKey();
		Entity entity=ClientDAO.loadEntity(userKey);
		String page=SystemConstants.ADMIN_USER_CONTROL_PAGE;
		if(((String)entity.getProperty("Role")).equalsIgnoreCase("user")){
			page=SystemConstants.USER_INDEX_PAGE;
		}
		RequestDispatcher dispatcher=req.getRequestDispatcher(page);
		dispatcher.forward(req, resp);
	}
}
